package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import com.google.gson.JsonObject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {
    Logger logger = LoggerFactory.getLogger(GoodsController.class);
    @DubboReference
    private TbItemCatService      tbItemCatService;
    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;
    @DubboReference
    private TbGoodsService tbGoodsService;
    @DubboReference
    private TbSpecificationService specificationService;
    /*商品管理页面跳转*/
    @RequestMapping("runGoods")
    public String runSeller(){
        return "admin/goods";
    }
    /*商品管理页面展示*/
    @RequestMapping("selectInfoLimitServer")
    @ResponseBody
    public PageResult<TbGoods> selectInfoLimitServer(TbGoods goods, @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        PageResult<TbGoods> pageBean = tbGoodsService.selectInfoLimitServer(goods,pageNumber, pageSize);
        return pageBean;
    }
    /*跳转商品添加*/
    @RequestMapping("getGoods")
    public String getGoods(){
        return "/admin/goods_edit";
    }

    /*商品*/
    @RequestMapping("addGoodsAndDesc")
    @ResponseBody
    public AjaxResult addGoodsAndDesc(TbGoods goods, TbGoodsDesc goodsDesc,String listItem){
        try {
            int i  = tbGoodsService.addGoodsAndDesc(goods,goodsDesc,listItem);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加商家信息失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }

    /*查询商品分类*/
    @RequestMapping("getItemCatById")
    @ResponseBody
    public AjaxResult getItemCatById(Long parentId){
        try {
            List<TbItemCat> itemCatList = tbItemCatService.getItemCatByParentId(parentId);
            if (itemCatList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(itemCatList);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("修改商家信息失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*更具商品id查找模板*/
    @RequestMapping("getItemCatByIdOnType")
    @ResponseBody
    public AjaxResult getItemCatByIdOnType(Long typeId){
        try {
            /*更具商品id查找对应的商品类型数据*/
            TbItemCat tbItemCat = tbItemCatService.getSelectItemcatById(typeId);
            /*更具商品的类型查找模板id,更具模板id查到对应的模板数据*/
            TbTypeTemplate template = tbTypeTemplateService.getSelctById(tbItemCat.getTypeId());
            if (template!=null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(template);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("更具商品id查找模板失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*更具规格id查找规格集合specificationService*/
    @RequestMapping("getSpecificationeBySpecId")
    @ResponseBody
    public AjaxResult getSpecificationeBySpecId(SpecVO specVO){
        try {
            List<TbSpecificationOption> optionList = specificationService.selectBySpecId(specVO.getId());
            specVO.setList(optionList);
            if (optionList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(specVO);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("更具规格id查找规格失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
}
